import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestCallable1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程开始执行...");

        // Executors.newSingleThreadExecutor()创建的是一个只有单个线程的线程池。
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<Integer> future = executorService.submit(new CallableExample());
        checkDone(future);

        Thread.sleep(1000);
        System.out.println("主线程继续执行中...");

        // future.cancel(true);

        Integer result = future.get();
        System.out.println("Callable的执行结果是：" + result);
        executorService.shutdown();
        checkDone(future);
    }

    public static <T> void checkDone(Future<T> future) {
        boolean done = future.isDone();
        System.out.println("是否已完成：" + done);
    }
}
